<?php  
    //定义Page分类页
    class Page {
        //定义属性
        public $firstRow;  //分页的起始位置(偏移量) 
        public $listRows;  //每页显示的记录数
        public $totalRows; //总记录数
        public $nowPage;   //当前页
        public $totalPage; //获取分页的总页数

        private $url;

        //定义构造器
        public function __construct($totalRows, $listRows = 10, $url = '') {
            //实现赋值操作
            $this->totalRows = $totalRows;
            $this->listRows = $listRows;
            $this->url = $url;
        }

        //定义核心方法，生成分页链接
        public function show() {
            //如果总记录数为0，则中止所有代码的执行
            if($this->totalRows == 0) {
                return '';
            }
            //求总页数
            $this->totalPage = ceil($this->totalRows / $this->listRows);
            //当前页
            $this->nowPage = isset($_GET['page']) ? intval($_GET['page']) : 1;
            //判断当前页是否越界
            if($this->nowPage < 1) {
                $this->nowPage = 1;
            }
            if($this->nowPage > $this->totalPage) {
                $this->nowPage = $this->totalPage;
            }

            //计算$firstRow偏移量
            $this->firstRow = ($this->nowPage - 1) * $this->listRows;
            

            //获取上一页与下一页
            $pagePrev = $this->nowPage - 1;
            $pageNext = $this->nowPage + 1;

            if($pagePrev == 0) {
                $pagePrev = 1;
            }

            if($pageNext > $this->totalPage) {
                $pageNext = $this->totalPage;
            }

            //仿百度分页程序
            $startNum = 1;   //1  2   3   4   5  6  7  8  9 10 //1    2   3   4   5
            $endNum = 1;

            if($this->nowPage <= 5) {
                $startNum = 1;
            } else {
                $startNum = $this->nowPage - 5;
            }

            if($startNum > $this->totalPage - 9) {
                $startNum = $this->totalPage - 9;
            }        

            if($startNum < 1) {
                $startNum = 1;
            }

            if($this->totalPage < 10){
                $endNum = $this->totalPage;
            }else{
                $endNum = $startNum + 9;

                if($endNum > $this->totalPage) {
                    $endNum = $this->totalPage;
                }
            }


            //生成数字页码
            $pageNum = '';
            for($i=$startNum;$i<=$endNum;$i++) {
                if($this->nowPage == $i) {
                    $pageNum.="<li class='paginItem current'><a href='{$this->url}?page={$i}'>{$i}</a></li>";
                } else {
                    $pageNum.="<li class='paginItem'><a href='{$this->url}?page={$i}'>{$i}</a></li>";
                }
            }

            //生成分页链接

$pageStr = <<<EOT
            <div class="pagin">
    	<div class="message">共<i class="blue">{$this->totalRows}</i>条记录，<i class="blue">{$this->totalPage}&nbsp;</i>页,当前显示第&nbsp;<i class="blue">{$this->nowPage}&nbsp;</i>页</div>
        <ul class="paginList">
        <li class="paginItem"><a href="{$this->url}?page={$pagePrev}"><span class="pagepre"></span></a></li>
         {$pageNum}
        <li class="paginItem"><a href="{$this->url}?page={$pageNext}"><span class="pagenxt"></span></a></li>
        </ul>
    </div>
EOT;
            //返回生成的分页链接
            return $pageStr;
        }
    }
?>